package tt.dz.egobus.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.annotation.LogAuto;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.common.AbstractFacadeFactoryService;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.manager.*;
import tt.dz.egobus.api.util.ResponseHelper;
import tt.dz.egobus.bean.Factory;
import tt.dz.egobus.bean.ServiceManagerI18NCodeEnum;
import tt.dz.egobus.cache.service.ExtendCacheService;
import tt.dz.egobus.dubbo.annotation.Interceptor;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.key.CodeBusinessIdGenerator;
import tt.dz.egobus.servicemanager.business.key.DriverStatus;
import tt.dz.egobus.servicemanager.business.lock.BusinessLock;
import tt.dz.egobus.servicemanager.business.lock.BusinessRedisLock;
import tt.dz.egobus.servicemanager.business.point.AddressPoint;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.entities.Bus;
import tt.dz.egobus.servicemanager.entities.BusinessService;
import tt.dz.egobus.servicemanager.entities.BusinessServiceInfo;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactory;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactoryLoggerInterceptorHolder;
import tt.dz.egobus.servicemanager.facade.order.OrderFacade;
import tt.dz.egobus.servicemanager.service.*;
import tt.dz.egobus.servicemanager.util.PojoConvertVoUtils;
import tt.dz.egobus.servicemanager.vo.BusinessFinishVo;
import tt.dz.egobus.servicemanager.vo.BusinessRequestVo;
import tt.dz.egobus.type.LogType;

import java.util.List;

import static tt.dz.egobus.api.util.ResponseHelper.copy;
import static tt.dz.egobus.bean.I18NCodeEnum.*;
import static tt.dz.egobus.bean.ServiceManagerI18NCodeEnum.*;

/**
 * @author Anson
 * @date 17-3-21
 * @name 管理api服务提供端实现
 * @since 1.0.2
 */
@Service(version = "1.0.2", interfaceClass = ManagerApiService.class)
public class ManagerApiServiceImpl extends AbstractFacadeFactoryService implements ManagerApiService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ManagerApiServiceImpl.class);

    @Override
    protected FacadeFactory getFacadeFactory() {
        return FacadeFactoryLoggerInterceptorHolder.LOGGER_FACADE;
    }


    @Interceptor
    @LogAuto(bizCode = 20090401, bizDesc = "司机上线", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> active(String busId, String driverId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(busId) || StringUtils.isEmpty(driverId)) {
                throw new ResponseException(RPC_PARAM_ENTITY_ERROR);
            }
            LOGGER.info("司机上线触发->当前获取司机={}", driverId);
            ManagerService managerService = getManagerService();
            ResponseBack<Boolean> responseBack = managerService.active(busId, driverId);
            LOGGER.info("司机上线触发->操作状态={}", responseBack.isSuccess());
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机上线触发->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090402, bizDesc = "司机下线", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> down(String driverId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(driverId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            LOGGER.info("司机下线触发->当前获取司机={}", driverId);
            ManagerService managerService = getManagerService();
            ResponseBack<Boolean> responseBack = managerService.down(driverId);
            LOGGER.info("司机下线触发->操作状态={}", responseBack.isSuccess());
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机下线触发->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    //@Interceptor
    @LogAuto(bizCode = 20090403, bizDesc = "司机上报地理位置", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> point(String driverId, double x, double y) {
        try {
            if (StringUtils.isEmpty(driverId)) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            if (x == 0 || y == 0) {
                return ResponseBack.success(false);
            }
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            businessWorkCacheService.updatePoint(driverId, x, y);
            return ResponseBack.success(true);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机上报地理位置->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @Override
    public ResponseBack<BusPointVo> get(String driverId) {
        try {
            if (StringUtils.isEmpty(driverId)) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            BusPoint busPoint = businessWorkCacheService.getNowPoint(driverId);
            if (null == busPoint) {
                return ResponseBack.error(this.get(RPC_PARAM_ENTITY_ERROR));
            }
            BusPointVo vo = copy(busPoint, new BusPointVo());
            return ResponseBack.success(vo);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("获取司机地理位置->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    /**
     * @param
     * @author Anson
     * @date 17-3-29
     * @name
     * @since 1.0.3
     */
    @Interceptor
    @Override
    public ResponseBack<DriverStatusVo> getStatus(String driverId) {
        try {
            if (StringUtils.isEmpty(driverId)) {
                return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
            }
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
            if (null == driverStatus) {
                return ResponseBack.success(null);
            }
            int statusCode = driverStatus.getStatus() == null ? -1 : driverStatus.getStatus().intValue();
            DriverRegisterBusRelative.RelativeStatus status =
                    DriverRegisterBusRelative.RelativeStatus.getStatus(statusCode);
            String busId = "";
            String busNo = "";
            if (DriverRegisterBusRelative.RelativeStatus.isAliveBindBus(statusCode)) {
                //若存在绑定的车辆信息的时候
                DriverRegisterBusRelativeService registerServiceService = getDriverRegisterBusRelativeService();
                List<DriverRegisterBusRelative> relatives = registerServiceService.queryValidateCarDriverBind(driverId);
                DriverRegisterBusRelative relative = null;
                if (!CollectionUtils.isEmpty(relatives)) {
                    relative = relatives.get(0);
                }
                Bus bus = null;
                if (relative != null && (bus = relative.getBus()) != null) {
                    busId = bus.getId();
                    busNo = bus.getCarNo();
                }
            }
            String msg = "";
            if (status != null) {
                msg = status.getMsg();
            }
            DriverStatusVo vo = new DriverStatusVo();
            vo.setDriverId(driverId);
            vo.setMsg(msg);
            vo.setStatus(driverStatus.getStatus());
            vo.setBusId(busId);
            vo.setBusNo(busNo);
            return ResponseBack.success(vo);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("获取司机状态->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090404, bizDesc = "用户查询司机地理位置", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusPointVo[]> queryNearbyBus(double x, double y, double maxDistanceLimit) {
        try {
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            //全查询，并筛选
            BusPoint[] busPoints = businessWorkCacheService.filterMaxLength(x, y, maxDistanceLimit, DriverRegisterBusRelative.RelativeStatus.ACTIVE);
            BusPointVo[] vos = new BusPointVo[0];
            if (busPoints != null) {
                vos = ResponseHelper.copy(busPoints, BusPointVo.class);
            }
            return ResponseBack.success(vos);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("用户查询司机地理位置->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090405, bizDesc = "用户请求派单,派单但未确认", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<DistributeDriverTask> requestTask(RequestTask requestTask) throws ResponseException {
        try {
            LOGGER.info("用户请求派单,派单但未确认->方法开始", requestTask);
            this.validateRequest(requestTask);
            String userId = requestTask.getUserId();
            UserRequestAddressInfo userRequestAddressInfo = requestTask.getUserRequestAddressInfo();
            AddressUnit userOnStartAddress = userRequestAddressInfo.getRequestOnStartAddress();
            double x = userOnStartAddress.getX();
            double y = userOnStartAddress.getY();
            String algorithmId = requestTask.getAlgorithmId();
            int businessCode = requestTask.getBusinessCode();
            LOGGER.info("用户请求派单,派单但未确认->当前获取用户={}", userId);
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            BusinessCacheService businessCacheService = this.getBusinessCacheService();
            DistributeDriverTask distributeDriverTask = new DistributeDriverTask();
            OrderFacade orderFacade = this.getOrderFacade();
            ResponseBack<Boolean> businessVerify = orderFacade.verifyOrderServeNumber(businessCode);
            //验证businessCode是否合法
            if ((!businessVerify.isSuccess() && !businessVerify.getResult())) {
                return ResponseBack.error(this.get(RPC_INVALIDATE_BUSINESS_NO_ERROR));
            }
            //验证当前系统中是否含有当前业务号
            BusPoint[] busPoints = businessWorkCacheService.bestSearch(x, y, businessCode, algorithmId);
            LOGGER.info("用户请求派单,派单但未确认->搜索到可用的司机个数={}", ArrayUtils.getLength(busPoints));
            /**
             * 业务id生成器
             */
            Factory<String> businessGenerator = new CodeBusinessIdGenerator(businessCode);
            //业务流水号生成器
            String businessId = businessGenerator.facorty();
            distributeDriverTask.setBusinessId(businessId);
            distributeDriverTask.setUserId(userId);
            if (busPoints != null && busPoints.length > 0) {
                distributeDriverTask.setPointVos(copy(busPoints, BusPointVo.class));
                businessCacheService.updateBusiness(requestTask, distributeDriverTask, busPoints);
            }
            return ResponseBack.success(distributeDriverTask);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("用户请求派单,派单但未确认->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090406, bizDesc = "调用者确认某个司机进行派单分发", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverAcceptAndBeginTask(String driverId, String businessId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(driverId) || StringUtils.isEmpty(businessId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            BusinessCacheService businessCacheService = this.getBusinessCacheService();
            BusinessCache businessCache = businessCacheService.get(businessId);
            //从缓存中获取当前业务配置缓存并且当前业务缓存集合中是否含有当前的司机缓存
            if (businessCache == null || !businessCache.validate()
                    || !businessCache.isContain(driverId)) {
                throw new ResponseException(RPC_NOT_FOUND_PRE_BUSINESS_CACHE_ERROR);
            }
            DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
            //确认当前司机流程为激活状态
            if (driverStatus == null || !DriverRegisterBusRelative.RelativeStatus.assertActive(driverStatus.getStatus())) {
                LOGGER.error("调用者确认某个司机进行派单分发触发->当前获取司机状态不是处于激活状态={}", driverStatus);
                throw new ResponseException(RPC_DRIVER_CAN_NOT_ACTIVE_ERROR);
            }
            BusPoint busPoint = businessWorkCacheService.getNowPoint(driverId);
            if (busPoint == null) {
                //当前的司机地理位置不存在的时候
                LOGGER.error("调用者确认某个司机进行派单分发触发->当前的司机地理位置不存在,driverId={}", driverId);
                throw new ResponseException(RPC_NOT_FOUND_DRIVER_UPLOAD_POINT_ERROR);
            }
            ManagerService managerService = getManagerService();
            //数据操作
            BusinessRequestVo businessRequestVo = BusinessRequestVo.instance(businessCache.getRequestTask());
            BusinessLock lock = new BusinessRedisLock(this.getExtendCacheService());
            ResponseBack<Boolean> responseBack = ResponseBack.error(this.get(RPC_DRIVER_GRAB_FAIL));
            try {
                if (lock.aquireLock()) {
                    responseBack = managerService.doBeginBusiness(driverId, businessId,
                            businessCache.getUserId(), busPoint, businessRequestVo);
                }
            } finally {
                lock.unlock();
            }
            LOGGER.info("调用者确认某个司机进行派单分发触发->操作状态={},businessId={},driverId={}",
                    responseBack.isSuccess(), businessId, driverId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("调用者确认某个司机进行派单分发触发->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090407, bizDesc = "司机确认身份之后,业务接人开始真正的业务", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverDoTask(DriverDoTaskRequest request) throws ResponseException {
        try {
            LOGGER.info("司机确认身份之后触发->方法开始");
            this.validateRequest(request);
            String driverId = request.getDriverId();
            String businessId = request.getBusinessId();
            AddressPoint addressPoint = PojoConvertVoUtils.driverDoTaskRequestConvetAddressPoint(request);
            LOGGER.info("司机确认身份之后触发->当前获取司机={}", driverId);
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            BusinessServiceService businessServiceService = this.getBusinessService();
            BusinessService businessService = businessServiceService.getBusinessByBusinessId(businessId);
            if (businessService == null) {
                //当前的业务流水号不存在的时候
                throw new ResponseException(RPC_PARAM_ENTITY_ERROR);
            }
            DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
            //确认当前司机流程为doBusiness，即接单任务准备中状态
            if (driverStatus == null
                    || !DriverRegisterBusRelative.RelativeStatus.assertBeginBusiness(driverStatus.getStatus())) {
                throw new ResponseException(RPC_DRIVER_NOT_IN_BEGIN_BUSINESS_ERROR);
            }
            LOGGER.info("司机确认身份之后->上传的地理位置为={}", addressPoint);
            //数据库数据操作
            ManagerService managerService = getManagerService();
            ResponseBack<Boolean> responseBack = managerService.doBusiness(driverId, businessService, addressPoint);
            LOGGER.info("司机确认身份之后->修改数据状态={},businessId={}", responseBack.isSuccess(), businessId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机确认身份之后->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090408, bizDesc = "获取当前业务的派单信息，只返回相关主键", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<TaskInfo> getTaskInfo(String businessId) throws ResponseException {
        try {
            ManagerService managerService = getManagerService();
            return managerService.getTaskInfo(businessId);
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("获取当前业务的派单信息->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090409, bizDesc = "司机完成整个业务最后的结束动作，完成此单的结束状态", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<BusinessVo> driverFinish(BusinessDetail detail) throws ResponseException {
        try {
            this.validateRequest(detail);
            BusinessWorkCacheService businessWorkCacheService = getBusinessWorkCacheService();
            String driverId = detail.getDriverId();
            LOGGER.info("司机接单完成触发->当前获取司机={}", driverId);
            DriverStatus driverStatus = businessWorkCacheService.getStatus(driverId);
            //确认当前司机流程为business，即接单任务中状态
            if (driverStatus == null
                    || (!DriverRegisterBusRelative.RelativeStatus.assertBusiness(driverStatus.getStatus()))) {
                LOGGER.error("司机接单完成触发->当前司机状态异常,driverId={},异常原因:status={}",
                        driverId, driverStatus == null ? "未绑定关系" : "不处于业务流程中");
                ServiceManagerI18NCodeEnum i18NCodeEnum =
                        driverStatus == null ? RPC_BUS_DRIVER_RELATIVE_NULL_BIND : RPC_DRIVER_NOT_IN_BUSINESS_ERROR;
                throw new ResponseException(i18NCodeEnum);
            }
            BusPoint busPoint = businessWorkCacheService.getNowPoint(driverId);
            if (busPoint == null) {
                //当前请求车辆地理位置为空
                throw new ResponseException(RPC_NOT_FOUND_DRIVER_UPLOAD_POINT_ERROR);
            }
            LOGGER.info("司机接单完成时候->当前获取司机的地理位置为{}", busPoint);
            BusinessFinishVo vo = PojoConvertVoUtils.businessDetailConvertBusinessFinishVo(detail);
            if (vo == null || !vo.validate()) {
                LOGGER.error("司机接单完成时候->请求参数转换业务参数验证失败:原始={},转换:={}", detail, vo);
                throw new ResponseException(RPC_SYSTEM_CODE_ERROR);
            }
            //数据库数据操作,并并生成订单信息
            ManagerService managerService = getManagerService();
            ResponseBack<BusinessServiceInfo> responseBack = managerService.doFinish(vo);
            if (responseBack.isSuccess()) {
                BusinessServiceInfo info = responseBack.getResult();
                LOGGER.info("司机接单完成时候->获取当前账单详细信息完成={}", info.getBusinessId());
                return ResponseBack.success(PojoConvertVoUtils.serviceInfoConvertBusinessVo(info));
            } else {
                LOGGER.error("司机接单完成时候->流程详细信息未正确完成businessId={}", detail.getBusinessId());
                return new ResponseBack(responseBack.getCode(), responseBack.getMsg());
            }
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机接单完成时候->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }


    @Interceptor
    @LogAuto(bizCode = 20090410, bizDesc = "用户取消了派单请求", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> userCancel(String userId, String businessId) throws ResponseException {
        try {
            if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(businessId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            LOGGER.info("用户取消触发->当前用户={},业务信息={}", userId, businessId);
            ManagerService managerService = getManagerService();
            //数据库更新流水信息
            ResponseBack<Boolean> isDoneResponse = managerService.doUserCancel(userId, businessId);
            LOGGER.info("用户取消触发->修改数据库取消的状态={},businessId={}", isDoneResponse.isSuccess(), businessId);
            return isDoneResponse;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("用户取消触发->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090411, bizDesc = "司机上报异常状态，会导致司机不能接单", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> driverException(ExceptionRequest request) throws ResponseException {
        try {
            this.validateRequest(request);
            LOGGER.info("司机上报异常状态->当前司机={}", request.getDriverId());
            ManagerService managerService = getManagerService();
            //数据库更新流水信息
            ResponseBack<Boolean> isDoneResponse =
                    managerService.doSaveBusinessExceptionAlert(request);
            LOGGER.info("司机上报异常状态->修改数据库取消的状态={},businessId={}", isDoneResponse.isSuccess(), request.getDriverId());
            return isDoneResponse;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("司机上报异常状态->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090412, bizDesc = "业务订单状态修改", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> modifyBusinessOrderStatus(String businessId, String orderId, int status) throws ResponseException {
        try {
            LOGGER.info("业务订单状态修改->当前业务={},订单编号={},状态={}", businessId, orderId, status);
            if (StringUtils.isEmpty(orderId)) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            ManagerService managerService = getManagerService();
            ResponseBack<Boolean> responseBack = managerService.modifyBusinessOrderStatus(businessId, orderId, status);
            LOGGER.info("业务订单状态修改->修改数据状态={},businessId={}", responseBack.isSuccess(), businessId);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("业务订单状态修改->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @LogAuto(bizCode = 20090413, bizDesc = "判断当前死否超出了最大距离", bizType = LogType.DUBBO)
    @Override
    public ResponseBack<Boolean> isOverMaxDistance(PointDistance pointDistance, int businessCode) {
        try {
            if (pointDistance == null || !pointDistance.validate()) {
                throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
            }
            LOGGER.info("判断当前死否超出了最大距离->当前距离={},businessCode={}", pointDistance, businessCode);
            ManagerService managerService = getManagerService();
            ResponseBack<Boolean> responseBack = managerService.isOverMaxDistance(pointDistance, businessCode);
            LOGGER.info("司机上报异常状态->判断是否超出的状态={},当前距离={},businessCode={}",
                    responseBack.isSuccess(), pointDistance, businessCode);
            return responseBack;
        } catch (ResponseException e) {
            return ResponseBack.error(e);
        } catch (RuntimeException e) {
            LOGGER.error("业务订单状态修改->系统异常", e);
            return ResponseBack.error(this.get(RPC_SYSTEM_CODE_ERROR));
        }
    }

    @Interceptor
    @Override
    public ResponseBack<List<BusinessCodeServiceStatus>> queryBusinessCodeServiceInfo() {
        ManagerService managerService = getManagerService();
        ResponseBack<List<BusinessCodeServiceStatus>> responseBack =
                managerService.queryBusinessCodeServiceInfo();
        return responseBack;
    }

    @Interceptor
    @Override
    public ResponseBack<Boolean> isBusinessCodeActive(int businessCode) {
        ManagerService managerService = getManagerService();
        ResponseBack<Boolean> responseBack =
                managerService.isBusinessCodeActive(businessCode);
        return responseBack;
    }

    @Interceptor
    @Override
    public ResponseBack<Boolean> isExitUserUnDoneBusiness(String userId) {
        if (userId == null) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        LOGGER.info("判断某个用户是否含有未完成的业务流程->userId={}", userId);
        ManagerService managerService = getManagerService();
        ResponseBack<Boolean> responseBack =
                managerService.isExitUserUnDoneBusiness(userId);
        LOGGER.info("判断啊某个用户是否含有未完成的业务流程->判断是否超出的状态={},userId={}",
                responseBack.isSuccess(), userId);
        return responseBack;
    }

    @Interceptor
    @Override
    public ResponseBack<TaskInfo> getUserUnDoneTaskInfo(String userId) {
        if (userId == null) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        LOGGER.info("用户当前未完成业务拍单信息->userId={}", userId);
        ManagerService managerService = getManagerService();
        return managerService.getUserUnDoneTaskInfo(userId);
    }

    @Interceptor
    @Override
    public ResponseBack<TaskInfo> getDriverUnDoneTaskInfo(String driverId) {
        if (driverId == null) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        LOGGER.info("司机当前未完成业务拍单信息->driverId={}", driverId);
        ManagerService managerService = getManagerService();
        return managerService.getDriverUnDoneTaskInfo(driverId);
    }

    @Override
    public ResponseBack<TaskInfo> getDriverLastestTaskInfo(String driverId) {
        if (driverId == null) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        LOGGER.info("司机当前最近一次派单信息->driverId={}", driverId);
        ManagerService managerService = getManagerService();
        return managerService.getDriverLastestTaskInfo(driverId);
    }

    @Override
    public ResponseBack<Integer> getDriverBusinesCount(String driverId) {
        if (driverId == null) {
            throw new ResponseException(RPC_PARAM_ILLEGAL_ERROR);
        }
        LOGGER.info("司机当前派单总数->driverId={}", driverId);
        ManagerService managerService = getManagerService();
        return managerService.getDriverBusinesCount(driverId);
    }

    @Override
    public ResponseBack<Integer[]> queryDriverBusinessCodeCount(Integer[] businessCodes, String driverId) {
        if (businessCodes == null || businessCodes.length == 0 || StringUtils.isEmpty(driverId)) {
            return ResponseBack.success(null);
        }
        LOGGER.info("计算司机的在不同的业务服务下的服务次数->driverId={},businesscodes={}", driverId, businessCodes);
        ManagerService managerService = getManagerService();

        return managerService.queryDriverBusinessCodeCount(businessCodes, driverId);
    }

    @Override
    public ResponseBack<Integer> getSystemAllMaxDistanceLength() {
        LOGGER.info("获取系统允许的最大范围的距离单位米/M");
        ManagerService managerService = getManagerService();
        return managerService.getSystemAllMaxDistanceLength();
    }

    @Interceptor
    @Override
    public ResponseBack<BusinessView> getBusinessView(String businessId) throws ResponseException {
        LOGGER.info("获取当前拍单视图信息,businessId={}", businessId);
        if (StringUtils.isBlank(businessId)) {
            return ResponseBack.success(null);
        }
        ManagerService managerService = getManagerService();
        BusinessView view = managerService.getServiceViewByBusinessId(businessId);
        return ResponseBack.success(view);
    }

    /**
     * @author Anson
     * @date 17-9-7
     * @name 获取缓存扩展服务
     * @since 1.0.7
     */
    ExtendCacheService getExtendCacheService() {
        return this.getService("extendsRedisServiceImpl", ExtendCacheService.class);
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取业务工作服务方法
     * @since 1.0.2
     */
    private BusinessWorkCacheService getBusinessWorkCacheService() {
        return this.getService("redisBusinessWorkCacheServiceImpl", BusinessWorkCacheService.class);
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取管理服务方法
     * @since 1.0.2
     */
    private ManagerService getManagerService() {
        return this.getService("managerServiceImpl", ManagerService.class);
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取业务服务服务方法
     * @since 1.0.2
     */
    private BusinessServiceService getBusinessService() {
        return this.getService("businessServiceServiceImpl", BusinessServiceService.class);
    }


    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取businessId缓存服务
     * @since 1.0.3
     */
    private BusinessCacheService getBusinessCacheService() {
        return this.getService("redisBusinessCacheServiceImpl", BusinessCacheService.class);
    }


    /**
     * 获取当前车辆注册的服务
     *
     * @return
     */
    private BusRegisterServiceService getBusRegisterServiceService() {
        return this.getService("busRegisterServiceServiceImpl", BusRegisterServiceService.class);
    }

    /**
     * @author Anson
     * @date 17-5-12
     * @name 获取当前司机注册服务
     * @since 1.0.4
     */
    private DriverRegisterBusRelativeService getDriverRegisterBusRelativeService() {
        return this.getService("driverRegisterBusRelativeServiceImpl", DriverRegisterBusRelativeService.class);
    }


    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取订单的facade
     * @since 1.0.3
     */
    OrderFacade getOrderFacade() {
        return this.facade("order", OrderFacade.class);
    }
}
